package api;

import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.cmd.Add;
import org.basex.core.cmd.AlterDB;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.Delete;
import org.basex.core.cmd.DropDB;
import org.basex.core.cmd.Open;
import org.basex.core.cmd.Set;
import org.basex.core.cmd.XQuery;

/**
* This class simplifies the usage of a BaseX (<a href="http://basex.org">http://basex.org</a>) database.
*
* @author Football-Statistics team
*/
public final class DatabaseManager {
    private String DBPath;
 
    public DatabaseManager() {
        this.DBPath = DatabaseManager.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace("DatabaseManager.class", "").replace("%20", " ")+"../../../BaseXDB";
    }
    /**
    * Sets the path, where the basex database should be
    * @param path absolute path to the DB
    * @throws BaseXException on database error
    */
    public void setDBPath(String path) {
        this.DBPath = path;
    }
    
    /**
    * Creates a collection
    * @param collection name of the collection
    * @throws BaseXException on database error
    */
    public void createCollection(String collection) throws BaseXException {
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        new CreateDB(collection).execute(context);
        context.close();
    }

    /**
    * Removes an existing collection
    * @param collection name of the collection
    * @throws BaseXException on database error
    */
    public void removeCollection(String collection) throws BaseXException {
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        //new Open(collection).execute(context);
        new DropDB(collection).execute(context);
        context.close();
    }

    /**
    * Renames an existing collection
    * @param collection name of the collection
    * @param newCollection new name
    * @throws BaseXException on database error
    */    
    public void renameCollection(String collection, String newCollection) throws BaseXException {
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        new AlterDB(collection, newCollection).execute(context);
        context.close();
    }

    /**
    * Adds a xml file to a collection
    * @param collection collection name
    * @param name name of the xml file (without extension)
    * @param xml well-formed XML string
    * @throws XMLDBException on database error
    */
    public void addXML(String collection, String name, String xml) throws BaseXException {
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        new Open(collection).execute(context);
        new Add(xml, name+".xml").execute(context);
        context.close();
    }

    /**
    * Removes a xml file from a collection
    * @param collection collection name
    * @param name name of the xml file (without extension)
    * @throws XMLDBException if a database command fails
    */
    public void removeXML(String collection, String name) throws BaseXException {
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        new Open(collection).execute(context);
        new Delete(name+".xml").execute(context);
        context.close();
    }

    /**
    * Runs a XPath or XQuery query on a collection
    * @param query the query as a String
    * @return String
    * @throws BaseXException on database error
    */
    public String queryCollection(String query) throws BaseXException {
        String toReturn;
        Context context = new Context();
        new Set("dbpath", this.DBPath).execute(context);
        toReturn = new XQuery(query).execute(context).toString();
        context.close();
        return toReturn;
    }
}